﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;
using YJKArchUtils.Utils;

namespace YJKRevitAxisModule
{
	public partial class AddAxisForm : System.Windows.Forms.Form
	{
		public string NewName { get; set; }

		public bool IsOffset { get; set; }

		public double OffsetLength { get; set; }

		public double RotateAngle { get; set; }

		public AddAxisForm(bool isOffset, ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.m_IsOffset = isOffset;
			this.InitializeComponent();
			this.tbRotate.Enabled = false;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.NewName = this.tbName.Text;
			if (this.rdbOffset.Checked)
			{
				this.IsOffset = true;
				try
				{
					this.OffsetLength = Convert.ToDouble(this.tbLength.Text);
					return;
				}
				catch (FormatException)
				{
					return;
				}
			}
			this.IsOffset = false;
			try
			{
				this.RotateAngle = Convert.ToDouble(this.tbRotate.Text);
			}
			catch (FormatException)
			{
			}
		}

		private void AxisNameForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (((System.Windows.Forms.Form)sender).DialogResult == DialogResult.OK)
			{
				string text = this.tbName.Text;
				if (text == "")
				{
					YJKMessageBox.Show("请输入轴号");
					e.Cancel = true;
					return;
				}
				string message = "";
				if (!AxisNameManager.IsValidatingOfName(text, true, ref message))
				{
					YJKMessageBox.Show(message);
					e.Cancel = true;
					return;
				}
				if (this.m_IsOffset && this.rdbRotate.Checked)
				{
					YJKMessageBox.Show("所选轴线无法旋转");
					e.Cancel = true;
					return;
				}
				if (this.rdbOffset.Checked)
				{
					try
					{
						double.Parse(this.tbLength.Text);
					}
					catch (FormatException)
					{
						YJKMessageBox.Show("偏移距离应该输入数字");
						e.Cancel = true;
						return;
					}
				}
				if (this.rdbRotate.Checked)
				{
					try
					{
						double.Parse(this.tbRotate.Text);
					}
					catch (FormatException)
					{
						YJKMessageBox.Show("偏转角度应该输入数字");
						e.Cancel = true;
					}
				}
			}
			this.SaveCompornentData();
		}

		private void rdbOffset_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbOffset.Checked)
			{
				this.tbLength.Enabled = true;
				this.tbRotate.Enabled = false;
			}
		}

		private void rdbRotate_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbRotate.Checked)
			{
				this.tbLength.Enabled = false;
				this.tbRotate.Enabled = true;
			}
		}

		private void AddAxisForm_Load(object sender, EventArgs e)
		{
			this.LoadCompornentData();
		}

		private void LoadCompornentData()
		{
			string path = Path.GetTempPath() + "AddAxisForm.dat";
			if (!File.Exists(path))
			{
				this.tbLength.Text = "1000";
				this.m_Length = this.tbLength.Text;
				this.rdbOffset.Checked = true;
				this.tbRotate.Text = "0";
				this.m_Angle = this.tbRotate.Text;
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			streamReader.ReadLine();
			this.tbName.Text = "";
			this.NewName = this.tbName.Text;
			this.rdbOffset.Checked = Convert.ToBoolean(streamReader.ReadLine());
			string text = streamReader.ReadLine();
			if (text.CompareTo("") == 0)
			{
				text = "1000";
			}
			this.tbLength.Text = text;
			this.m_Length = this.tbLength.Text;
			this.rdbRotate.Checked = Convert.ToBoolean(streamReader.ReadLine());
			text = streamReader.ReadLine();
			if (text.CompareTo("") == 0)
			{
				text = "0";
			}
			this.tbRotate.Text = text;
			this.m_Angle = this.tbRotate.Text;
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveCompornentData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "AddAxisForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.tbName.Text);
			streamWriter.WriteLine(this.rdbOffset.Checked);
			streamWriter.WriteLine(this.tbLength.Text);
			streamWriter.WriteLine(this.rdbRotate.Checked);
			streamWriter.WriteLine(this.tbRotate.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void tbLength_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbLength.Text, 0.0, 100000.0))
			{
				YJKMessageBox.Show("偏移距离应该输入大于0的数字");
				this.tbLength.Text = this.m_Length;
				return;
			}
			this.m_Length = this.tbLength.Text;
		}

		private void tbRotate_TextChanged(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbRotate.Text, 0.0, 360.0))
			{
				YJKMessageBox.Show("角度应在0到360之间");
				this.tbRotate.Text = this.m_Angle;
				return;
			}
			this.m_Angle = this.tbRotate.Text;
		}

		private void tbName_TextChanged(object sender, EventArgs e)
		{
		}

		private ExternalCommandData m_Revit;

		private bool m_IsOffset = true;

		private string m_Length = "";

		private string m_Angle = "";
	}
}
